/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package invitation;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import model.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import validate.AccountValidate;

/**
 *
 * @author tomasz
 */
@Controller
@RequestMapping("/create/invitation.htm")
public class CreateInvitationForm {
    
    
    private dao.Firma firma;
    private dao.Account account;
    
    @Autowired
    public void setFirma(dao.Firma firma){
        this.firma = firma;
    }
    
    @Autowired
    public void setAccount(dao.Account account){
        this.account = account;
    }
    
    
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    
    @ModelAttribute("list_firm")
    public List<model.Firma> populateCountryList(HttpServletRequest request) {
        
          List firma = this.firma.allFirmaInvi();
        
          return firma;
    }
    
    @RequestMapping(method = RequestMethod.GET)
    public String CreateInvitation(Model model) {
        

        Account account = new Account();
        model.addAttribute(account);
        
        return "invitation/create_invitation_form";
        
    }
    
    
    @RequestMapping(method= RequestMethod.POST)
    public String processSubmit(@ModelAttribute Account account, BindingResult result,HttpServletRequest request ){
        
       
        new AccountValidate().validate(account, result);
        
        if (result.hasErrors()) {
            
		return "invitation/create_invitation_form";
                
	} else {
            
                
                String start_date = request.getParameter("dateStartFirma");
                
                int year = Integer.parseInt( start_date.substring(0, 4) );
                int month = Integer.parseInt( start_date.substring(5, 7) );
                int day = Integer.parseInt( start_date.substring(8, 10) );
                
                Calendar ca = Calendar.getInstance();
                ca.set(year,month-1,day);
                Date ca_start_date = ca.getTime();
                account.setDateStartFirma(ca_start_date);
                
                
                model.Firma firma = new model.Firma();
                firma.setIdFirma(Integer.parseInt( request.getParameter("id_f")));
                account.setFirma(firma);
                account.setStatus(1);
            
                
                model.Content con = new model.Content();
                con.setDescription(account.getContent().getDescription());
                con.getAccounts().add(account);
                
                account.setContent(con);
                
                this.account.addContent(con);
                
                return "redirect:/list/invitation.htm";
                
	}
        
    }
    
}
